#if 0
#include "cremote.h"
#include "cmaster.h"

#include <buola/io/cipendpoint.h>

namespace buola { namespace bboxd {

CRemote::CRemote(CMaster *pMaster,io::PSocket pSocket)
    :   CConnection(pSocket,pMaster->UUID())
    ,   mMaster(pMaster)
{
}

CRemote::CRemote(CMaster *pMaster,const std::string &pPeerHost)
    :   CConnection(new io::CSocket(io::CIPEndPoint(pPeerHost,9595)),pMaster->UUID())
    ,   mMaster(pMaster)
    ,   mPeerHost(pPeerHost)
{
}

CRemote::~CRemote()
{
    mMaster->UnregisterRemote(this);
}

void CRemote::FindClient(const std::string &pName,std::vector<std::string> &pClients)
{
    bbox::protocol::CStackPacket lPacket(bbox::protocol::OP_FINDCLIENT);
    lPacket << pName;
    SendPacket(lPacket);

    std::unique_ptr<bbox::protocol::CPacket> lReply(WaitReply(lPacket));

    std::size_t lCount=lReply->Pop<std::size_t>();

    pClients.clear();

    for(int i=0;i<lCount;i++)
    {
        std::string lNew;
        lReply->Pop(lNew);
        pClients.push_back(lNew);
    }
}

void CRemote::FindBoxes(const std::string &pClient,const std::string &pBox,
                            std::vector<std::tuple<std::string,std::string,uint64_t>> &pBoxes)
{
    CStackPacket lPacket(OP_FINDBOX);
    lPacket.Push(pClient);
    lPacket.Push(pBox);
    SendPacket(lPacket);

    std::unique_ptr<bbox::protocol::CPacket> lReply(WaitReply(lPacket));

    std::size_t lCount=lReply->Pop<std::size_t>();
    
    pBoxes.clear();
    
    for(int i=0;i<lCount;i++)
    {
        std::string lAddress,lName;
        uint64_t lID;
        lReply->Pop(lAddress);
        lReply->Pop(lName);
        lReply->Pop(lID);
        pBoxes.emplace_back(lAddress,lName,lID);
    }
}

bool CRemote::BindPeerConnection(uint64_t pID,const std::string &pName,const std::string &pThisPeer)
{
    CStackPacket lPacket(OP_BINDPEER);
    lPacket.Push(pID);
    lPacket.Push(pName);
    lPacket.Push(pThisPeer);
    SendPacket(lPacket);

    std::unique_ptr<bbox::protocol::CPacket> lReply(WaitReply(lPacket));

    std::size_t lCount=lReply->Pop<std::size_t>();

    return lCount;
}

void CRemote::ProcessFindClient(bbox::protocol::CPacket *pPacket)
{
    std::string lName;
    pPacket->Pop(lName);
    
    std::vector<CBBoxClient*> lClients;
    mMaster->FindClients(lName,lClients);

    CReplyPacket lReply(pPacket);

    lReply.Push((std::size_t)lClients.size());

    for(std::vector<CBBoxClient*>::iterator i=lClients.begin();i!=lClients.end();i++)
        lReply.Push((*i)->GetAddress()+'@'+mMaster->GetUUID());

    SendPacket(lReply);
}

void CRemote::ProcessFindBox(bbox::protocol::CPacket *pPacket)
{
    std::string lClient;
    pPacket->Pop(lClient);
    std::string lName;
    pPacket->Pop(lName);

    std::vector<std::tuple<std::string,std::string,uint64_t>> lResults;
    mMaster->FindBoxes(lClient,lName,lResults);

    CReplyPacket lReply(pPacket);

    lReply.Push((std::size_t)lResults.size());

    for(const auto &b : lResults)
    {
        lReply.Push(std::get<0>(b));
        lReply.Push(std::get<1>(b));
        lReply.Push(std::get<2>(b));
    }

    SendPacket(lReply);
}

void CRemote::ProcessClientList(bbox::protocol::CPacket *pPacket)
{
    CReplyPacket lReply(pPacket);
    
    const std::vector<CBBoxClient*> &lClients=mMaster->GetAllClients();
    
    lReply.Push((std::size_t)lClients.size());
    
    for(std::vector<CBBoxClient*>::const_iterator i=lClients.begin();i!=lClients.end();i++)
        lReply.Push((*i)->GetAddress());
    
    SendPacket(lReply);
}

void CRemote::ProcessBindPeer(bbox::protocol::CPacket *pPacket)
{
    std::string lAddress;
    uint64_t lID;
    std::string lRemoteAddress;
    pPacket->Pop(lID);
    pPacket->Pop(lAddress);
    pPacket->Pop(lRemoteAddress);

    CBBoxClient* lClient=mMaster->GetClient(lAddress);
    io::PSocket lSocket=mMaster->GetPeerConnection(lID);
        
    CReplyPacket lReply(pPacket);

    if(lClient&&lSocket)
    {
        //we found the client the other host wants to connect to... so pass it down to the client
        lClient->SendPeerSocket(lSocket,lRemoteAddress,false);
        
        lReply.Push((std::size_t)1);
    }
    else
    {
        lReply.Push((std::size_t)0);
    }
    
    SendPacket(lReply);
}

void CRemote::ProcessRequest(bbox::protocol::CPacket *pPacket)
{
    switch(pPacket->GetMajorOpcode())
    {
    case OP_FINDCLIENT:
        ProcessFindClient(pPacket);
        break;
    case OP_FINDBOX:
        ProcessFindBox(pPacket);
        break;
    case OP_CLIENTLIST:
        ProcessClientList(pPacket);
        break;
    case OP_BINDPEER:
        ProcessBindPeer(pPacket);
        break;
    default:
        msg_warn() << "received unknown packet with opcode=" << pPacket->GetMajorOpcode() << "... ignoring it\n";
    }
}

void CRemote::OnEOF()
{
    delete this;
}

void CRemote::OnHelloReceived(const std::string &pPeerName)
{
    mPeerUUID=pPeerName;

    mMaster->RegisterRemote(this,mPeerHost,mPeerUUID);
}

/*namespace bboxd*/ } /*namespace buola*/ }
#endif